﻿using SmartRFIDSystem.dao;
using SmartRFIDSystem19.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmartRFIDSystem19.view
{
    public partial class RegisterFrom : Form
    {
        private string cap="";
        public RegisterFrom()
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void ok_Click(object sender, EventArgs e)
        {
            if(txt_userName.Text.Equals("")||txt_email.Text.Equals("")||txt_captcha.Text.Equals("")||txt_password.Text.Equals("")||confirm.Text.Equals(""))
            {
                MessageBox.Show("内容为空，请填写完整");
            }
            else if (cap.Equals(""))
            {
                MessageBox.Show("请发送验证码");
            }
            else if (!txt_password.Text.Equals(confirm.Text))
            {
                MessageBox.Show("两次密码不一致");
            }

            //判断验证码是否正确
            else if (!model.Captcha.CheckCode(cap, this.txt_captcha.Text))
            {
                MessageBox.Show("验证码错误！");
            }
            else if (!IsPasswordValid(txt_password.Text))
            {
                MessageBox.Show("密码最少为6位！并且包含数字和字母！");
            }

            else
            {
                UserDao user = new UserDao();
                if (user.register(txt_userName.Text, txt_password.Text, txt_email.Text) == 1)
                {
                    this.Close();
                }
            }

        }

        private void send_Click(object sender, EventArgs e)
        {
            //邮箱地址合法性校验
            if (!model.Mail.checkEmail(txt_email.Text))
            {
                MessageBox.Show("邮箱地址不合法！");
                return;
            }
            //生成五位数的验证码
            cap = model.Captcha.GetRandomCode(5);
            //将五位数的验证码发送到填写的邮箱
            Mail.sendMail(txt_email.Text, "智慧一卡通系统注册验证码", "本次验证码为：" + cap);
        }

        private void RegisterFrom_Load(object sender, EventArgs e)
        {

        }

        private void txt_password_TextChanged(object sender, EventArgs e)
        {

        }
        public static bool IsPasswordValid(string password)
        {
            if (password.Length < 6)
            {
                // 密码长度小于6位
                return false;
            }

            bool hasDigit = false;
            bool hasLetter = false;

            foreach (char c in password)
            {
                if (char.IsDigit(c))
                {
                    hasDigit = true;
                }
                else if (char.IsLetter(c))
                {
                    hasLetter = true;
                }
            }

            // 判断是否同时包含数字和字母
            if (!hasDigit || !hasLetter)
            {
                return false;
            }

            return true;
        }

    }
}
